<?php
/**
 * 新华扬人事授权，接收微信信息请求管理类
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/9
 * Time: 10:10
 */

namespace app\api\controller\v1;


use app\api\service\WorkBoss;
use app\api\validate\WorkNotifyParameter;
use app\api\validate\WorkParameter;
use app\api\validate\WorkPermanentCodeParameter;
use \app\api\service\Token;
use app\lib\exception\SuccessMessage;
use app\lib\exception\TokenException;
use app\lib\exception\WorkException;
use app\lib\exception\WorkInfoException;

class Boss
{
    /**
     * 用于返回新华扬人事应用的通信标识，验证url的有效性接收get请求
     * 以及接收微信返回的post请求。获取数据
     * 此应用没有权限限制，对外的公共方法。用于接收微信推送的suite_ticket通知
     * @return mixed|string
     */
        public function getBossSuit(){
            $notify = new WorkBoss();
            if (request()->isGet()){
                (new WorkNotifyParameter())->goCheck();
                $sEchoStr =  $notify->get();
                return $sEchoStr;
            }else if (request()->isPost()){
                (new WorkParameter())->goCheck();
                $sEchoStr =  $notify->getNotify();
                if ($sEchoStr){
                    return 'success';
                }
            }
        }
    /**
     * 接收新华扬人事的事件与消息。验证url有效性时为get请求
     * 以及接收微信返回的post请求的事件和消息信息。
     * 此方法没有权限限制，对外的公共方法。用于处理事件和消息信息
     * @return string
     * @throws MiniSuiteException
     */
    public function getEventInfo($CORPID){
        //error_log(print_r($CorpID, 1), 3, "./userinfo");
        $notify = new WorkBoss();
        if (request()->isGet()){
            (new WorkNotifyParameter())->goCheck();
            $sEchoStr =  $notify->get();
            return $sEchoStr;
        }else if (request()->isPost()){
            (new WorkParameter())->goCheck();
            $sEchoStr =  $notify->getEventInfo($CORPID);
            if ($sEchoStr){
                return 'success';
            }
        }
    }
    /**
     * 获取预授权码
     * @return \ServiceCorpAPI
     */
    public function getPreAuthCode(){
        $notify = new WorkBoss();
        $result =  $notify->getPreAuthCode();
        return $result;
   }
    /**
     * 设置授权配置
     */
    public function setAppConfig(){
        $notify = new WorkBoss();
        $result = $notify->setAppConfig();
        return $result;
    }
    /**
     * 获取永久授权码
     * @param int $code
     * @return \GetPermanentCodeRsp
     */
    public function getPermanentCode($code=0){
        (new WorkPermanentCodeParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $contact = new WorkBoss();
        $result = $contact->getTestPermanentCode($code);
        return $result;
    }
    /**
     * 获取通讯录授权url
     * @return string
     */
    public function getBossAuthUrl(){
        $token = Token::getCurrentTokenValue();
        $contact = new WorkBoss();
        $url =  $contact->getAuthUrl($token);
        return $url;
    }
    /**
     *接收微信回调
     * @param $auth_code
     * @param $state
     * @return \think\response\Json
     * @throws TokenException
     * @throws WorkException
     * @throws WorkInfoException
     */
    public function receiveUri($auth_code,$expires_in,$state){
        if (Token::verifyToken($state)) {
            $userInfo = Token::getCurrentToken($state);
            $uid = $userInfo['uid'];
            $contact = new WorkBoss();
            $result = $contact->getPermanentCode($auth_code);
            if (!$result) {
                throw new WorkException([
                    'msg' => '企业微信获取永久授权信息失败'
                ]);
            }
            $type['type'] = 'boss';
            $info = (new WorkBoss())->authorizeBoss($uid, $result,$type);
            if (!$info) {
                throw new WorkInfoException();
            } else {
                return json(new SuccessMessage());
            }
        }else{
            throw new TokenException([
                'msg'=>'token检测不通过,请重新授权',
            ]);
        }
    }
}